﻿<?php
  ob_clean();
  $id = $_GET['id'];//获取地址栏中的参数名是Id的值
  //ajiax发送请求删除分类
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])){
      $sql = "select * from `cate` where `pid` = $id ";//查找是否有子级
      $result = mysqli_query($GLOBALS['link'],$sql);
      echo ($result->num_rows>0)?1:0;die;
  }
  $rows = getcatedel($id);
  if ($rows>0) {
        echo '<script>alert("删除成功");location.href="index.php?c=cate&a=list&admin=1"</script>';die;
  }else{
        echo '<script>alert("删除失败");history.go(-1);</script>';die;
   }

  function getcatedel($pid=0){
      $sql = "select * from `cate` where `pid` = $pid ";
      $result = mysqli_query($GLOBALS['link'],$sql);
      if (($result->num_rows)>0) {
        while ($rows = mysqli_fetch_assoc($result)){
           getcatedel($rows['id']);
        }
      }
    $sql = "delete from `cate` where `id` = $pid ";
    $result = mysqli_query($GLOBALS['link'],$sql);
    return mysqli_affected_rows($GLOBALS['link']);
  }


?>


